package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.HuanXin.HuanXinTemplate;

import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendService;
import com.itheima.service.mongo.UserLIkeService;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserLikeManager {

    @DubboReference
    private UserLIkeService userLIkeService;

    @DubboReference
    private FriendService friendService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private RecommendService recommendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //卡片右滑
    public void cardLove(Long likeUserId) {
        Long userId = UserHolder.get().getId();
        //往userLike表中存关系
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis());
        userLike.setLikeUserId(likeUserId);
        userLike.setUserId(userId);
        userLIkeService.saveUserLike(userLike);

        //判断是否是相互喜欢，相互喜欢要加好友
        if (userLIkeService.isMutualLike(userId, likeUserId)) {
            //双向添加好友
            friendService.addFriend(userId, likeUserId);
            //环信添加好友
            huanXinTemplate.addContacts(userId, likeUserId);
        }

        //删除recommend_user
        recommendService.removeCard(userId, likeUserId);
    }


    //左滑卡片
    public void cardUnLove(Long likeUserId) {
        Long userId = UserHolder.get().getId();
        //判断是否是相互喜欢的关系，friend
        if (userLIkeService.isMutualLike(userId, likeUserId)) {
            //则删除该关系
            friendService.removeFriend(userId, likeUserId);
            huanXinTemplate.deleteContacts(userId, likeUserId);
        }
        //删除喜欢关系
        userLIkeService.removeUserLike(userId, likeUserId);

        //删除推荐关系,给谁推荐自己，推荐谁，喜欢
        recommendService.removeCard(userId, likeUserId);
    }

    //统计关系
    public ResponseEntity findUserLikeCountVo() {
        Long userId = UserHolder.get().getId();
        UserLikeCountVo userLikeCountVo = userLIkeService.findUserLikeCountVo(userId);

        return ResponseEntity.ok(userLikeCountVo);
    }

    //我的详情
    public ResponseEntity myDetail(Integer type, Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();

        //查出pbv
        PageBeanVo pageBeanVo = userLIkeService.findUserLikeVoByType(userId, type, pageNum, pageSize);
        List<UserLikeVo> vos =(List<UserLikeVo>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(vos)) {
            for (UserLikeVo vo : vos) {
                UserInfo userInfo = userInfoService.findById(vo.getId());
                BeanUtil.copyProperties(userInfo, vo );

                //粉丝栏里，是否喜欢他，高亮
                vo.setAlreadyLove(userLIkeService.isMutualLike(userId,vo.getId()));
            }
        }
        return ResponseEntity.ok(pageBeanVo);
    }
}
